package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBeautyVoice;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserBeautyVoiceDaoImpl extends TempDataObjectDao<UserBeautyVoice> {
    public static String getUserBeautyVoiceListSql = "select * from `t_u_beauty_voice` where `userId`=?";
    public Map<Integer,UserBeautyVoice> getUserBeautyVoiceMap(long userId){
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer,UserBeautyVoice> userBeautyVoiceMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserBeautyVoiceListSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                userBeautyVoiceMap.put(rs.getInt("beautyId"),parse(rs)) ;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return userBeautyVoiceMap;

    }




    public static String getUserBeautyVoiceSql = "select * from `t_u_beauty_voice` where `userId`=? and `beautyId`=?";
    public UserBeautyVoice getUserBeautyVoice(long userId,int beautyId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserBeautyVoiceSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, beautyId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserBeautyVoiceSql = "update `t_u_beauty_voice` set voiceList=?,redDotVoiceList=?,chooseVoice=? where userId=? and beautyId=?";
    @Override
    protected boolean updateData(UserBeautyVoice userBeautyVoice) {
        userBeautyVoice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userBeautyVoice.getVoiceList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userBeautyVoice.getRedDotVoiceList())));
        para.put(i++, new DbParameter(Types.INTEGER, userBeautyVoice.getChooseVoice()));
        para.put(i++, new DbParameter(Types.BIGINT, userBeautyVoice.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userBeautyVoice.getBeautyId()));
        if (getDb().executeNoneQuery(updateUserBeautyVoiceSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserBeautyVoiceSql = "insert into `t_u_beauty_voice`(userId,beautyId,voiceList,redDotVoiceList,chooseVoice) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserBeautyVoice userBeautyVoice) {
        userBeautyVoice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userBeautyVoice.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userBeautyVoice.getBeautyId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userBeautyVoice.getVoiceList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userBeautyVoice.getRedDotVoiceList())));
        para.put(i++, new DbParameter(Types.INTEGER, userBeautyVoice.getChooseVoice()));
        if (getDb().executeNoneQuery(insertUserBeautyVoiceSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserBeautyVoice parseData(ResultSet rs) throws SQLException {
        UserBeautyVoice userBeautyVoice = new UserBeautyVoice();
        userBeautyVoice.setUserId(rs.getLong("userId"));
        userBeautyVoice.setBeautyId(rs.getInt("beautyId"));
        userBeautyVoice.setVoiceList(JSON.parseObject(rs.getString("voiceList"), new TypeReference<List<Integer>>() {}));
        userBeautyVoice.setRedDotVoiceList(JSON.parseObject(rs.getString("redDotVoiceList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userBeautyVoice.setChooseVoice(rs.getInt("chooseVoice"));

        return userBeautyVoice;
    }
}
